package top.huntwolf.admin.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.huntwolf.admin.common.JsonResult;
import top.huntwolf.admin.pojo.PurchaseVo;
import top.huntwolf.admin.pojo.Trade;
import top.huntwolf.admin.pojo.book.Book;
import top.huntwolf.admin.pojo.book.BuyBook;
import top.huntwolf.admin.service.*;

import java.util.List;

@Api(description = "书本控制器")
@RestController
@CrossOrigin
@RequestMapping(value = "/book")
public class BookController {

    @Autowired
    private BookService bookService;

    @Autowired
    private HistoryService searchService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private BookReserveService conditionService;

    @GetMapping("/{isbn}")
    @ApiOperation("通过本地数据库获取书籍")
    public JsonResult<BuyBook> queryBook(@PathVariable String isbn) {

        BuyBook book = bookService.searchBookByISBN(isbn);
        return JsonResult.success(book);

    }

//    @GetMapping("/fromApi/{isbn}")
//    @ApiOperation("通过易源接口获取书籍")
//    public JsonResult<BuyBook> queryBookByApi(@PathVariable String isbn){
//
//        BuyBook book = bookService.getBookDetailsFromApi(isbn);
//        return JsonResult.success(book);
//
//    }

    @GetMapping("/bySearchSaleDescend/search/{searchField}/pageNum/{pageNum}")
    @ApiOperation("作者和书名以销量降序排列(暂时默认)")
    public JsonResult<List<BuyBook>> queryBookOrderSaleDesc(@PathVariable String searchField,@PathVariable int pageNum){
        searchService.addSearchHistory(searchField);
        List<BuyBook> books = bookService.searchBookOrderSaleDesc(searchField,pageNum);
        return JsonResult.success(books);
    }

    @GetMapping("/bySearchSaleRise/search/{searchField}/pageNum/{pageNum}")
    @ApiOperation("作者和书名以销量正序排列")
    public JsonResult<List<BuyBook>> queryBookOrderSaleAsc(@PathVariable String searchField,@PathVariable int pageNum){
        List<BuyBook> books = bookService.searchBookOrderSaleAsc(searchField,pageNum);
        return JsonResult.success(books);
    }

    @GetMapping("/bySearchPriceDescend/search/{searchField}/pageNum/{pageNum}")
    @ApiOperation("作者和书名以价格降序排列")
    public JsonResult<List<BuyBook>> queryBookOrderPriceDesc(@PathVariable String searchField,@PathVariable int pageNum){
        List<BuyBook> books = bookService.searchBookOrderPriceDesc(searchField,pageNum);
        return JsonResult.success(books);
    }

    @GetMapping("/bySearchPriceRise/search/{searchField}/pageNum/{pageNum}")
    @ApiOperation("作者和书名以价格正序排列")
    public JsonResult<List<BuyBook>> queryBookOrderPriceAsc(@PathVariable String searchField,@PathVariable int pageNum){
        List<BuyBook> books = bookService.searchBookOrderPriceAsc(searchField,pageNum);
        return JsonResult.success(books);
    }

    // 根据分类查询课本
    @GetMapping("/bySubjectSaleDescend/subjectId/{subjectId}/pageNum/{pageNum}")
    @ApiOperation("分类以销量降序排列(暂时默认)")
    public JsonResult queryBookBySubject(@PathVariable Integer subjectId,@PathVariable int pageNum) {

        List<BuyBook> bookList =  bookService.searchBookBySubjectOrderSaleDesc(subjectId,pageNum);
        return JsonResult.success(bookList);

    }

    @GetMapping("/bySubjectSaleRise/subjectId/{subjectId}/pageNum/{pageNum}")
    @ApiOperation("分类以销量正序排列")
    public JsonResult queryBookBySubjectOrderSaleAsc(@PathVariable Integer subjectId,@PathVariable int pageNum) {

        List<BuyBook> bookList =  bookService.searchBookBySubjectOrderSaleAsc(subjectId,pageNum);
        return JsonResult.success(bookList);

    }

    @GetMapping("/bySubjectPriceDescend/subjectId/{subjectId}/pageNum/{pageNum}")
    @ApiOperation("分类以价格降序排列")
    public JsonResult queryBookBySubjectOrderPriceDesc(@PathVariable Integer subjectId,@PathVariable int pageNum) {

        List<BuyBook> bookList =  bookService.searchBookBySubjectOrderPriceDesc(subjectId,pageNum);
        return JsonResult.success(bookList);

    }

    @GetMapping("/bySubjectPriceRise/subjectId/{subjectId}/pageNum/{pageNum}")
    @ApiOperation("分类以价格正序排列")
    public JsonResult queryBookBySubjectOrderPriceAsc(@PathVariable Integer subjectId,@PathVariable int pageNum) {

        List<BuyBook> bookList =  bookService.searchBookBySubjectOrderPriceAsc(subjectId,pageNum);
        return JsonResult.success(bookList);

    }

    @GetMapping("/byCreateTime/{pageNum}")
    @ApiOperation("获取最新上架书籍")
    public JsonResult queryBookByCreateTime(@PathVariable int pageNum){
        List<BuyBook> books = bookService.searchBookByCreateTime(pageNum);
        return JsonResult.success(books);
    }


    // 批量添加图书
    @ApiOperation("批量添加图书")
    @PostMapping("/batch")
    public JsonResult addBookBatch(MultipartFile file){
        bookService.addBookBatch(file,bookService,conditionService);

        return JsonResult.success(null);
    }

    @GetMapping("/hotList/{pageNum}")
    @ApiOperation("热销列表")
    public JsonResult queryBookBySaleLastMonth(@PathVariable int pageNum){
        List<Book> books = bookService.searchAllHotSale(pageNum);
        return JsonResult.success(books);
    }

    @GetMapping("/transaction/{isbn}")
    @ApiOperation("获取交易量")
    public JsonResult<Trade> queryBookByIsbnTransaction(@PathVariable String isbn){
        Trade trade = orderService.searchBuyBookByIsbnTransaction(isbn);
        return JsonResult.success(trade);
    }

    @GetMapping("/recent")
    @ApiOperation("获取最近购买")
    public JsonResult<List<PurchaseVo>> queryBookByRecent(){
        return JsonResult.success(orderService.searchRecentBuyBook());
    }

}
